/*
 * CLASS: AdminManageProducts.java
 */

package view;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.DefaultTableModel;
import model.*;

/**
 *
 * @author Samuele Mattiuzzo
 */
public class AdminManageProducts extends javax.swing.JFrame {

    Library library;
    MainPage main;
    /** Creates new form AdminManageProducts */
    public AdminManageProducts(Library l, MainPage m) {
        library = l;
        main = m;
        initComponents();
        populateAuthors();
        populateGenre();
        populateTypes();
        refreshTable();
    }

    private void tableReset(int size){
        DefaultTableModel tbl = (DefaultTableModel)jTable1.getModel();
        for(int i = 0;i < size;i++)
            tbl.removeRow(0);
    }
    /**
     * Aggiorna la tabella degli autori
     */
    private void refreshTable(){
        int size = jTable1.getRowCount();
        DefaultTableModel tbl = (DefaultTableModel)jTable1.getModel();
        tableReset(size);
        for(Product p:library.getProducts())
            tbl.addRow(new Object[]{p.getProductId(),p.getTitle(),p.getType(),p.getAuthors()});
    }

    /**
     * Controlla se il form di inserimento e' compilato correttamente
     * @return false se uno dei campi e' vuoto
     */
    private boolean checkValid(){
        if(!jTextTitle.getText().isEmpty() &&
                (!jTextType.getText().isEmpty() || !(jComboType.getSelectedIndex() <= -1)) &&
                (!jTextGenre.getText().isEmpty()|| !(jComboGenre.getSelectedIndex() <= -1))&&
                (!(jComboAuthor.getSelectedIndex() <= -1)) &&
                (!jTextDescr.getText().isEmpty()))
            return true;
        return false;
    }
     /**
     * Popola la combo box degli autori
     */
    private void populateAuthors(){
        DefaultComboBoxModel tmp = (DefaultComboBoxModel)jComboAuthor.getModel();
        tmp.addElement("");
        for(Author a:library.getAuthors())
            tmp.addElement(a.toString());
    }

    /**
     * Popola la combo box dei generi
     */
    private void populateGenre(){
        DefaultComboBoxModel tmp = (DefaultComboBoxModel)jComboGenre.getModel();
        tmp.addElement("");
        for(Product p:library.getProducts()){
          boolean ins = true;
          for(int i = 0; i < jComboGenre.getItemCount(); i++){
            if(jComboGenre.getItemAt(i).equals(p.getGenre())){
              ins = false;
              break;
            }
          }
          if(ins)
              tmp.addElement(p.getGenre());
        }
    }

    /**
     * Popola la combo box dei tipi
     */
    private void populateTypes(){
        DefaultComboBoxModel tmp = (DefaultComboBoxModel)jComboType.getModel();
        tmp.addElement("");
        for(Product p:library.getProducts()){
          boolean ins = true;
          for(int i = 0; i < jComboType.getItemCount(); i++){
            if(jComboType.getItemAt(i).equals(p.getType())){
              ins = false;
              break;
            }
          }
          if(ins)
              tmp.addElement(p.getType());
        }
    }
    
    /**
     * Aggiunge un nuovo prodotto alla collezione
     * @param t titolo
     * @param g genere
     * @param d descrizione
     * @param a autori
     * @param k tipo
     */
    private void addProduct(String t, String g, String d, Author a, String k){
        int id = library.getProducts().size();
        library.getProducts().add(new Product(id,t,g,d,a,k));
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jTextTitle = new javax.swing.JTextField();
        jTextType = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jComboAuthor = new javax.swing.JComboBox();
        jTextGenre = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextDescr = new javax.swing.JTextField();
        jComboType = new javax.swing.JComboBox();
        jComboGenre = new javax.swing.JComboBox();
        jButtonDelete = new javax.swing.JButton();
        jCloseButton = new javax.swing.JButton();
        jButtonDetails = new javax.swing.JButton();
        jButtonConfirm = new javax.swing.JButton();

        jDialog1.setTitle("DETTAGLI ");
        jDialog1.setBounds(new java.awt.Rectangle(0, 22, 300, 350));

        org.jdesktop.layout.GroupLayout jDialog1Layout = new org.jdesktop.layout.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jDialog1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                .addContainerGap())
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jDialog1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
                .addContainerGap())
        );

        setTitle("GESTIONE PRODOTTI");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "ID", "Titolo", "Tipo", "Autore"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setResizable(false);
        jTable1.getColumnModel().getColumn(1).setResizable(false);
        jTable1.getColumnModel().getColumn(2).setResizable(false);
        jTable1.getColumnModel().getColumn(3).setResizable(false);

        jLabel1.setText("TITOLO");

        jLabel2.setText("TIPO");

        jLabel3.setText("AUTORE");

        jLabel4.setText("GENERE");

        jTextDescr.setText("Descrizione");

        jButtonDelete.setText("ELIMINA");
        jButtonDelete.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonDeleteMouseClicked(evt);
            }
        });

        jCloseButton.setText("INDIETRO");
        jCloseButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jCloseButtonMouseClicked(evt);
            }
        });

        jButtonDetails.setText("DETTAGLI");
        jButtonDetails.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonDetailsMouseClicked(evt);
            }
        });

        jButtonConfirm.setText("CONFERMA");
        jButtonConfirm.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonConfirmMouseClicked(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 666, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(layout.createSequentialGroup()
                                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                            .add(layout.createSequentialGroup()
                                                .add(jLabel1)
                                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                                .add(jTextTitle, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 154, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                            .add(layout.createSequentialGroup()
                                                .add(jLabel3)
                                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                                .add(jComboAuthor, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 152, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                                        .add(18, 18, 18)
                                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                            .add(jLabel4)
                                            .add(jLabel2))
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                            .add(layout.createSequentialGroup()
                                                .add(jTextType, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 154, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                                .add(18, 18, 18)
                                                .add(jComboType, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                            .add(layout.createSequentialGroup()
                                                .add(jTextGenre, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 154, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                                .add(18, 18, 18)
                                                .add(jComboGenre, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jButtonConfirm)))
                                    .add(layout.createSequentialGroup()
                                        .add(jButtonDetails)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(jButtonDelete)))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))
                            .add(jTextDescr, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 340, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jCloseButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 210, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(jButtonConfirm)
                        .add(jCloseButton))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jButtonDetails)
                            .add(jButtonDelete))
                        .add(20, 20, 20)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                    .add(jLabel1)
                                    .add(jTextTitle, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jComboAuthor, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(jLabel3)))
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                    .add(jTextType, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(jLabel2)
                                    .add(jComboType, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                    .add(jLabel4)
                                    .add(jTextGenre, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(jComboGenre, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                        .add(27, 27, 27)
                        .add(jTextDescr, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jCloseButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jCloseButtonMouseClicked
        this.setVisible(false);
    }//GEN-LAST:event_jCloseButtonMouseClicked

    /**
     * Dettagli di un prodotto
     * @param evt
     */
    private void jButtonDetailsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonDetailsMouseClicked
        if(jTable1.getSelectedRow() > -1){
        Integer i = (Integer) jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 0);
        jLabel5.setText(library.getProducts().get(i).displayInfo());
        jDialog1.setVisible(true);
        }
    }//GEN-LAST:event_jButtonDetailsMouseClicked

    /**
     * Rimozione di un prodotto
     * @param evt
     */
    private void jButtonDeleteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonDeleteMouseClicked
        if(jTable1.getSelectedRow() > -1){
        Integer i = (Integer) jTable1.getModel().getValueAt(jTable1.getSelectedRow(), 0);
        Product tmp = library.getProducts().get(i);
        library.getProducts().remove(tmp);
        jLabel5.setText("Prodotto rimosso");
        jDialog1.setVisible(true);
        refreshTable();
        }
    }//GEN-LAST:event_jButtonDeleteMouseClicked

    /**
     * Inserimento di un prodotto
     * @param evt
     */
    private void jButtonConfirmMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonConfirmMouseClicked
        if(checkValid()){
            String title = jTextTitle.getText();
            String genre = new String();
            String type = new String();
            String descr = new String();
            Author au = null;

            if(jTextGenre.getText().isEmpty())
                genre = jComboGenre.getSelectedItem().toString();
            else
                genre = jTextGenre.getText();

            if(jTextType.getText().isEmpty())
                type = jComboType.getSelectedItem().toString();
            else
                type = jTextType.getText();

            descr = jTextDescr.getText();

            for(Author a: library.getAuthors())
                if(a.toString().equals(jComboAuthor.getSelectedItem().toString()))
                    au = a;

            addProduct(title,genre,descr,au,type);
            jLabel5.setText("Prodotto inserito.");
            jDialog1.setVisible(true);
            refreshTable();

        }else{
            jLabel5.setText("Compilare tutti i campi.");
            jDialog1.setVisible(true);
        }
    }//GEN-LAST:event_jButtonConfirmMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonConfirm;
    private javax.swing.JButton jButtonDelete;
    private javax.swing.JButton jButtonDetails;
    private javax.swing.JButton jCloseButton;
    private javax.swing.JComboBox jComboAuthor;
    private javax.swing.JComboBox jComboGenre;
    private javax.swing.JComboBox jComboType;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextDescr;
    private javax.swing.JTextField jTextGenre;
    private javax.swing.JTextField jTextTitle;
    private javax.swing.JTextField jTextType;
    // End of variables declaration//GEN-END:variables

}
